/**
 * Created by KhoaNam on 7/12/2014.
 */
var MySprite = cc.Sprite.extend({
    ctor: function(){
        this._super();
        this.enableEvent = true;
        this.isTap = false;
        this.isMouseOver = false;
        this.src = "";
        this.nextOrder = 0;
    },

    init: function(){
        this._super();
    },

    setImage: function(source){
        if(source != null){
            this.src = source;
            var texture = cc.TextureCache.getInstance().textureForKey(this.src);
            this.setTexture(texture);
        }
    },

    moveTo: function(duration, pos, callback){
        var move = cc.MoveTo.create(duration, cc.p(pos.x, pos.y));
        var action = cc.Sequence.create(move, cc.CallFunc.create(function(){
            this.setZOrder(order);
            if(callback){
                callback(this);
            }
        }, this));
        var order = this.getZOrder();
        /*action.onFinished = function(target){
            target.setZOrder(order);
            if(callback){
                callback(target);
            }
        };*/
        this.setZOrder(INFO.TILE.ORDER_ANIMATION);
        this.runAction(action);
    },

    moveBy: function(duration, pos, callback){
        var move = cc.MoveBy.create(duration, cc.p(this.getPositionX() + pos.x, this.getPositionY() + pos.y));
        var action = cc.Sequence.create(move, cc.CallFunc.create(function(){
            this.setZOrder(order);
            if(callback){
                callback(this);
            }
        }, this));
        var order = this.getZOrder();
        /*action.onFinished = function(target){
            target.setZOrder(order);
            if(callback){
                callback(target);
            }
        };*/
        this.setZOrder(INFO.TILE.ORDER_ANIMATION);
        this.runAction(action);
    },

    easeExponentialOutTo : function(duration, pos, callback){
        var move = cc.MoveTo.create(duration, cc.p(pos.x, pos.y));
        var easeout = cc.EaseExponentialOut.create(move);
        var action = cc.Sequence.create(easeout, cc.CallFunc.create(function(){
            this.setZOrder(order);
            if(callback){
                callback(this);
            }
        }, this));
        var order = this.getZOrder();
        /*action.onFinished = function(target){
            target.setZOrder(order);
            if(callback){
                callback(target);
            }
        };*/
        this.setZOrder(INFO.TILE.ORDER_ANIMATION);
        this.runAction(action);
    },

    animationTo: function(x, y, callback){
        var posDistance = {};
        posDistance.x = x - this.getPositionX();
        posDistance.y = y - this.getPositionY();

        var distance = Math.sqrt(posDistance.x*posDistance.x + posDistance.y*posDistance.y);
        var half  = distance/2;
        var t1 = half/INFO.ANIMATION.SPEED;
        var t2 = half/(INFO.ANIMATION.SPEED*2);

        var move1 = cc.MoveBy.create(t1, cc.p( posDistance.x/2, posDistance.y/2));
        var move2 = cc.MoveBy.create(t2, cc.p( posDistance.x/2, posDistance.y/2));

        var action = cc.Sequence.create(move1, move2, cc.CallFunc.create(function(){
            this.setZOrder(order);
            if(callback){
                callback(this);
            }
        }, this));
        var order = this.getZOrder();
        /*action.onFinished = function(target){
            target.setZOrder(order);
            if(callback){
                callback(target);
            }
        };*/
        this.setZOrder(INFO.TILE.ORDER_ANIMATION);
        this.runAction(action);
    },

    getDurationBySpeed: function(x, y, speed){
        var posDistance = {};
        posDistance.x = x - this.getPositionX();
        posDistance.y = y - this.getPositionY();

        var distance = Math.sqrt(posDistance.x*posDistance.x + posDistance.y*posDistance.y);

        var t = distance/speed;
        return t;
    },

    onTouchBegan: function(touch, event){
        this.isTap = true;
    },

    onTouchEnded: function(touch, event){
        if(this.isTap){
            this._onTap(touch, event);
        }
    },

    onTouchMoved: function(touch, event){
        this.isTap = false;
    },

    onTouchCancelled: function(touch, event){
        this.isTap = false;
    },

    onTap: function(touch, event){

    },

    _onTap: function(touch, event){
        this.onTap(touch, event);
    },

    _onMouseOver: function(mouse, event){
        this.onMouseOver(mouse, event);
    },

    onMouseOver: function(mouse, event){

    },

    _onMouseOut: function(mouse, event){
        this.onMouseOut(mouse, event);
    },

    onMouseOut: function(mouse, event){

    }
});